import { queryVersionsByProject } from '../services/project';
import {addVerion, deleteVersion, updateVersion,getProjectVersionList} from '../services/version'
import moment from 'moment';

export default {
  namespace: 'version',

  state: {
    list: [],
    visible: false,
    projectId:0,  
    editingKey:'',
    currentVersion:{},
    versionList:[]
  },

  effects: {
    *showVersionsListModal({ payload }, { call, put }) {
      let params = { page: 1, size: 10, projectId: payload };
      let response = yield call(queryVersionsByProject, params);
      yield put({
        type: 'setList',
        payload: response.code == 0 ? response.data:[]
      });
      yield put({
        type: 'setProjectId',
        payload:payload
      })
      yield put({
        type: 'setVisible',
        payload: true
      })
    },
    *queryByProjectId({ payload }, { call, put }){
      let response = yield call(queryVersionsByProject, {projectId: payload});
      if(response.code === 0){
        yield put({
          type: 'setList',
          payload: response.code == 0 ? response.data : []
        });
        yield put({
          type:'setCurrentVersion',
          payload: response.data[0]
        })
      }
      
    },
    *add({ params }, { call, put, select }) {
      let dateFormat = 'YYYY-MM-DD';
      const projectId = yield select(state => state.version.projectId);
      params = {
        projectId: projectId,
        name: params.name,
        expectTestTime: params.expectTestTime.format(dateFormat),
        expectVersionReleaseTime: params.expectVersionReleaseTime.format(dateFormat),
        selfTestCaseUrl: params.selfTestCaseUrl
      }
      let response = yield call(addVerion, params);
      if(response.code === 0){
        yield put({
          type: 'appendList',
          payload: {
            id: response.data.versionId,
            key:response.data.versionId,
            ...params
          }
        })
      }  
    },
    *edit({ params }, { call, put, select }) {
      let list = yield select(state => state.version.list);
      let dateFormat = 'YYYY-MM-DD';
      params = {
        id: params.id,
        name: params.name,
        expectTestTime: params.expectTestTime.format(dateFormat),
        expectVersionReleaseTime: params.expectVersionReleaseTime.format(dateFormat),
        selfTestCaseUrl: params.selfTestCaseUrl
      }
      let transParams = {...params};
      // let versionInList = list.filter((version) => {
      //   return version.id == transParams.id;
      // })
      // if(versionInList && transParams.expectTestTime == transParams.expectTestTime){
      //   delete transParams.expectTestTime;
      // }

      let response = yield call(updateVersion, transParams);
      if(response.code === 0){
        list = list.map( version => {
          if(version.id === params.id){
            return params;
          }else{
            return version;
          }
        })
        yield put({
          type: 'setList',
          payload: list
        })
        yield put({
          type: 'setEditingKey',
          payload:''
        }) 
      }
       
    },
    *delete({ params }, { call, put }) {
      let response = yield call(deleteVersion, params);
      if(response.code === 0){
        yield put({
          type:'popVersionFromList',
          payload: params
        })
        yield put({
          type: 'setEditingKey',
          payload:''
        }) 
      }
    },
    *queryVersionList({payload},{call,put}){
      let response = yield call(getProjectVersionList,payload);
      if(response.code === 0){
        yield put({
          type:'setVersionList',
          payload:response.data
        })
      }
    }
  },

  reducers: {
    setList(state, action) {
      let listdata = action.payload;
      listdata = listdata.map(version => {
        return _.merge(version, { key: version.id })
      });
      return {
        ...state,
        list: listdata
      }
    },
    appendList(state, action){
      return {
        ...state,
        list: state.list.concat(action.payload),
      };
    },
    popVersionFromList(state, action){
      let list = _.remove(state.list, (version) => {
        return version.id !== action.payload
      })
      return {
        ...state,
        list: list
      };
    },
    setVisible(state, action) {
      let editingKey = state.editingKey;
      if(action.payload == false){
        editingKey='';
      }
      return {
        ...state,
        editingKey:editingKey,
        visible: action.payload
      }
    },
    setProjectId(state, action){
      return {
        ...state,
        projectId: action.payload
      }
    },
    setEditingKey(state, action){
      return {
        ...state,
        editingKey: action.payload
      }
    },
    setCurrentVersion(state, action){
      return {
        ...state,
        currentVersion: action.payload
      }
    },
    setVersionList(state,action){
      return{
        ...state,
        versionList:action.payload
      }
    },
  }
};
